$.ajax({
	type:'get',
	url:'/comments',
	success:function(res){
		console.log(res);
		var html = template('commentsTpl',res)
		$('#commentsBox').html(html)

		var pageHtml = template('pageTpl',res)
		$('#pageBox').html(pageHtml)
	}
})

function changePage(page){
	console.log(page);
	$.ajax({
		type:'get',
		url:'/comments',
		data:{
			page:page 
		},
		success:function(res){
			console.log(res);
			var html = template('commentsTpl',res)
			$('#commentsBox').html(html)

			var pageHtml = template('pageTpl',res)
			$('#pageBox').html(pageHtml)
		}
	})
}

//事件委托来给审批按钮添加点击事件
$('#commentsBox').on('click','.state',function(){
	var id = $(this).attr('data-id');
	var state = $(this).attr('data-state');
	state = state==1?0:1//取反一下
	$.ajax({
		type:'put',
		url:`/comments/${id}`,
		data:{
			state:state
		},
		success:function(){
			location.reload();
		}
	})
})

//用事件委托给删除按钮添加点击事件
$('#commentsBox').on('click','.delete',function(){
	if(confirm('确定要删除吗?')){
		var id = $(this).attr('data-id')
			$.ajax({
				type:'delete',
				url:`/comments/${id}`,
				success:function(){
					location.reload()
				}
			})
		}
})